package com.dianping.cat.home.utilization.transform;

import com.dianping.cat.home.utilization.IVisitor;
import com.dianping.cat.home.utilization.entity.ApplicationState;
import com.dianping.cat.home.utilization.entity.Domain;
import com.dianping.cat.home.utilization.entity.MachineState;
import com.dianping.cat.home.utilization.entity.UtilizationReport;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  21 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
/*  26 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(UtilizationReport utilizationReport) {
/*  30 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(utilizationReport, out);
/*  33 */     return out.toByteArray();
  }


/*  37 */   public static void build(UtilizationReport utilizationReport, OutputStream out) { utilizationReport.accept(new DefaultNativeBuilder(out)); }



  public void visitApplicationState(ApplicationState applicationState) {
/*  42 */     if (applicationState.getId() != null) {
/*  43 */       writeTag(1, 1);
/*  44 */       writeString(applicationState.getId());
    }

/*  47 */     writeTag(2, 0);
/*  48 */     writeLong(applicationState.getFailureCount());

/*  50 */     writeTag(3, 0);
/*  51 */     writeDouble(applicationState.getFailurePercent());

/*  53 */     writeTag(4, 0);
/*  54 */     writeLong(applicationState.getCount());

/*  56 */     writeTag(5, 0);
/*  57 */     writeDouble(applicationState.getMaxQps());

/*  59 */     writeTag(6, 0);
/*  60 */     writeDouble(applicationState.getAvg());

/*  62 */     writeTag(7, 0);
/*  63 */     writeDouble(applicationState.getSum());

/*  65 */     writeTag(8, 0);
/*  66 */     writeDouble(applicationState.getAvg95());

/*  68 */     writeTag(63, 3);
  }


  public void visitDomain(Domain domain) {
/*  73 */     if (domain.getId() != null) {
/*  74 */       writeTag(1, 1);
/*  75 */       writeString(domain.getId());
    }

    writeTag(2, 0);
/*  79 */     writeInt(domain.getMachineNumber());

/*  81 */     if (domain.getCmdbId() != null) {
/*  82 */       writeTag(3, 1);
/*  83 */       writeString(domain.getCmdbId());
    }

/*  86 */     if (!domain.getMachineStates().isEmpty()) {
/*  87 */       writeTag(33, 2);
/*  88 */       writeInt(domain.getMachineStates().size());

/*  90 */       for (MachineState machineState : domain.getMachineStates().values()) {
/*  91 */         machineState.accept(this.m_visitor);
      }
    }

    if (!domain.getApplicationStates().isEmpty()) {
/*  96 */       writeTag(34, 2);
/*  97 */       writeInt(domain.getApplicationStates().size());

/*  99 */       for (ApplicationState applicationState : domain.getApplicationStates().values()) {
/* 100 */         applicationState.accept(this.m_visitor);
      }
    }

/* 104 */     writeTag(63, 3);
  }


  public void visitMachineState(MachineState machineState) {
/* 109 */     if (machineState.getId() != null) {
/* 110 */       writeTag(1, 1);
/* 111 */       writeString(machineState.getId());
    }

/* 114 */     writeTag(2, 0);
/* 115 */     writeLong(machineState.getCount());

/* 117 */     writeTag(3, 0);
/* 118 */     writeDouble(machineState.getSum());

/* 120 */     writeTag(4, 0);
/* 121 */     writeDouble(machineState.getAvg());

/* 123 */     writeTag(5, 0);
/* 124 */     writeDouble(machineState.getAvgMax());

/* 126 */     writeTag(63, 3);
  }


  public void visitUtilizationReport(UtilizationReport utilizationReport) {
/* 131 */     writeTag(63, 0);

/* 133 */     if (utilizationReport.getDomain() != null) {
/* 134 */       writeTag(1, 1);
/* 135 */       writeString(utilizationReport.getDomain());
    }

/* 138 */     if (utilizationReport.getStartTime() != null) {
/* 139 */       writeTag(2, 1);
/* 140 */       writeDate(utilizationReport.getStartTime());
    }

/* 143 */     if (utilizationReport.getEndTime() != null) {
/* 144 */       writeTag(3, 1);
/* 145 */       writeDate(utilizationReport.getEndTime());
    }

/* 148 */     if (!utilizationReport.getDomains().isEmpty()) {
/* 149 */       writeTag(33, 2);
/* 150 */       writeInt(utilizationReport.getDomains().size());

/* 152 */       for (Domain domain : utilizationReport.getDomains().values()) {
/* 153 */         domain.accept(this.m_visitor);
      }
    }

/* 157 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 162 */       writeVarint(value.getTime());
/* 163 */     } catch (Exception e) {
/* 164 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 170 */       writeVarint(Double.doubleToLongBits(value));
/* 171 */     } catch (Exception e) {
/* 172 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 178 */       writeVarint(value & 0xFFFFFFFFL);
/* 179 */     } catch (Exception e) {
/* 180 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 186 */       writeVarint(value);
/* 187 */     } catch (Exception e) {
/* 188 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 194 */       this.m_out.writeUTF(value);
/* 195 */     } catch (IOException e) {
/* 196 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 202 */       this.m_out.writeByte((field << 2) + type);
/* 203 */     } catch (IOException e) {
/* 204 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 210 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 211 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 214 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 215 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/utilization/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */